package com.syj.jsxjj.ui.renzheng.fragment;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.syj.jsxjj.R;
import com.syj.jsxjj.app.App;
import com.syj.jsxjj.base.BaseFragment;
import com.syj.jsxjj.bean.BaseResponse;
import com.syj.jsxjj.http.HttpManager;
import com.syj.jsxjj.http.HttpSubscriber;
import com.syj.jsxjj.ui.renzheng.activity.AuthEmergencyLinkManActivity;
import com.syj.jsxjj.ui.renzheng.activity.AuthenPersonalInformationActivity;
import com.syj.jsxjj.ui.renzheng.activity.PhoneActivity;
import com.syj.jsxjj.ui.renzheng.bean.AuthenticationBean;
import com.syj.jsxjj.ui.renzheng.bean.ContentBean;
import com.syj.jsxjj.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Scheduler;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * author：wangzuzhen on 2016/9/21 0021 12:15
 * 还款
 */
public class AuthenFragment extends BaseFragment implements View.OnClickListener {
    public static AuthenFragment sFragment;
    public static final String CONTENT_BEAN = "content";
    private ContentBean content;
    @BindView(R.id.rl_relation)
    RelativeLayout mInfoLayout;
    @BindView(R.id.tv_relation)
    TextView mInfoStatu;

    @BindView(R.id.rl_phone)
    RelativeLayout mPhoneLayout;
    @BindView(R.id.tv_phone)
    TextView mPhoneStatu;

    @BindView(R.id.rl_link_layout)
    RelativeLayout mLinkManLayout;
    @BindView(R.id.tv_linkText)
    TextView mLinkManStatu;


    public static AuthenFragment getInstance() {
        if (sFragment == null)
            sFragment = new AuthenFragment();
        return sFragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_authen;
    }

    @Override
    public void initPresenter() {
    }

    @Override
    public void loadData() {
        initTitle();
        initView();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }

    private void initTitle() {
        mTitle.setTitle(false, "认证");
    }

    @Override
    public void onResume() {
        super.onResume();
        initView();
    }

    private Subscription subscribe;

    private void initView() {
        if (!App.getConfig().getLoginStatus()) {
            return;
        }
        subscribe = HttpManager.getStringApi().queryCertification("1").subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HttpSubscriber<String>() {

                    @Override
                    protected void _onStart() {

                    }

                    @Override
                    protected void _onNext(String s) {
                        s = s.replace("\\", "").replace("\"{", "{").replace("}\"", "}");
                        Log.e("result=", s);
                        try {
                            JSONObject response = new JSONObject(s);
                            if (response.getString("code").equals("0")) {
                                AuthenticationBean bean = new Gson().fromJson(response.getString("result"), AuthenticationBean.class);
                                content = bean.getContent();
                                Log.e("result=", bean.toString());
                                if (content != null) {
                                    if (content.getPersonal() != null && !"{}".equals(content.getPersonal())) {
                                        mInfoStatu.setText("已填写");
                                    } else {
                                        mInfoStatu.setText("未填写");
                                    }
                                    if (content.getContract() != null && !"{}".equals(content.getContract())) {
                                        mLinkManStatu.setText("已填写");
                                    } else {
                                        mLinkManStatu.setText("未填写");
                                    }
                                    if (content.getOperator() != null && !"{}".equals(content.getOperator())) {
                                        mPhoneStatu.setText("已填写");
                                    } else {
                                        mPhoneStatu.setText("未填写");
                                    }
                                }

                            } else {
                                ToastUtil.showToast(response.getString("message"));
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    protected void _onError(String message) {

                    }

                    @Override
                    protected void _onCompleted() {

                    }
                });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        sFragment = null;
        if (subscribe != null && !subscribe.isUnsubscribed()) {
            subscribe.unsubscribe();
        }
    }

    @OnClick({R.id.rl_relation, R.id.rl_link_layout, R.id.rl_phone})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_relation:
                Intent intent = new Intent(getActivity(), AuthenPersonalInformationActivity.class);
                intent.putExtra(CONTENT_BEAN, content);
                startActivity(intent);
                break;
            case R.id.rl_link_layout:
                Intent linkIntent = new Intent(getActivity(), AuthEmergencyLinkManActivity.class);
                linkIntent.putExtra(CONTENT_BEAN, content);
                startActivity(linkIntent);
                break;
            case R.id.rl_phone:
                Intent phoneIntent = new Intent(getActivity(), PhoneActivity.class);
                phoneIntent.putExtra(CONTENT_BEAN, content);
                startActivity(phoneIntent);
                break;
            default:
                break;
        }
    }
}
